/*
 * @Author: 大D
 * @email: 857216393@qq.com
 * @Date: 2023-12-27 10:16:51
 * @Description:  全局loading
 * @params  IOptions | string
 * load.show(IOptions | string)  load.hide()
 */
import loadingPage from '@/components/loading/loading.vue';

import { createApp } from 'vue';
interface IOptions {
  vnode?: string;
  text?: string;
  color?: string;
  bgColor?: string;
}
let loadNum = 0;
const parentNode = document.createElement('div');
let createNode: any;
function show(o?: IOptions | string) {
  let options: IOptions = {};
  if (typeof o === 'string') {
    options.text = o;
  }
  if (typeof o === 'object') {
    options = o;
  }
  loadNum++;
  if (loadNum == 1) {
    createNode = createApp(loadingPage, { ...options });
    document.body.appendChild(parentNode);
    createNode.mount(parentNode);
  }
}
function hide() {
  loadNum--;
  if (loadNum == 0) {
    createNode.unmount();
    document.body.removeChild(parentNode);
  }
}
const loading = {
  show,
  hide
};
export default loading;
